package com.hanhai;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Set;

/**
 * @author weijingchen
 * @DateTime 2025-05-20 10:21
 */
public class JDK8Date {
    public static void main(String[] args) {
        //ZoneIDTest();
        //instanceTest();
        //ZonedDateTimeTest();
        DateTimeFormatterTest();
    }

    //时间格式化
    private static void DateTimeFormatterTest() {

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(ZonedDateTime.now());

        LocalDateTime  localDateTime = LocalDateTime.now();
        String format1 = dateTimeFormatter.format(localDateTime);
        System.out.println(format1);
        System.out.println(format);//2025-05-20 12:11:18
    }

    private static void ZonedDateTimeTest() {
        //1、获取当前时间对象
        ZonedDateTime now = ZonedDateTime.now();//2025-05-20T11:56:21.286272300+08:00[Asia/Shanghai]
        System.out.println(now);

        //2、获取指定时间对象
        ZonedDateTime zonedDateTime = ZonedDateTime.of(2025, 5, 20,
                                                            11, 56, 21,
                                            286272300, ZoneId.of("Asia/Shanghai"));
        System.out.println(zonedDateTime);

        //3、通过Instant+时区的方式获取时间对象
        Instant instant = Instant.now();
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        ZonedDateTime zonedDateTime1 = ZonedDateTime.ofInstant(instant, zoneId);
        System.out.println(zonedDateTime1);

        //4、withXxx 修改时间系例方法
        //将年份修改为2020年
        ZonedDateTime zonedDateTime2 = zonedDateTime.withYear(2020);
        System.out.println(zonedDateTime2);

        //将日期设置为当年的1月1号
        ZonedDateTime zonedDateTime3 = zonedDateTime.withDayOfYear(1);
        System.out.println(zonedDateTime3);
        //将日期设置为当月的1号
        ZonedDateTime zonedDateTime4 = zonedDateTime.withDayOfMonth(1);
        System.out.println(zonedDateTime4);
        //将时区设置为中国时区
        ZonedDateTime zonedDateTime5 = zonedDateTime.withEarlierOffsetAtOverlap();
        System.out.println(zonedDateTime5);

        //5、minusXxx 减少
        ZonedDateTime zonedDateTime6 = zonedDateTime.minusYears(1);
        System.out.println(zonedDateTime6);
        ZonedDateTime zonedDateTime7 = zonedDateTime.minusMonths(1);
        System.out.println(zonedDateTime7);
        ZonedDateTime zonedDateTime8 = zonedDateTime.minusDays(1);
        System.out.println(zonedDateTime8);
        ZonedDateTime zonedDateTime9 = zonedDateTime.minusHours(1);
        System.out.println(zonedDateTime9);
        ZonedDateTime zonedDateTime10 = zonedDateTime.minusMinutes(1);
        System.out.println(zonedDateTime10);
        ZonedDateTime zonedDateTime11 = zonedDateTime.minusSeconds(1);
        System.out.println(zonedDateTime11);


        //6、plusXxx 增加
        ZonedDateTime zonedDateTime12 = zonedDateTime.plusYears(1);
        System.out.println(zonedDateTime12);
        ZonedDateTime zonedDateTime13 = zonedDateTime.plusMonths(1);
        System.out.println(zonedDateTime13);
        ZonedDateTime zonedDateTime14 = zonedDateTime.plusDays(1);
        System.out.println(zonedDateTime14);
        ZonedDateTime zonedDateTime15 = zonedDateTime.plusHours(1);
        System.out.println(zonedDateTime15);
        ZonedDateTime zonedDateTime16 = zonedDateTime.plusMinutes(1);
        System.out.println(zonedDateTime16);
        ZonedDateTime zonedDateTime17 = zonedDateTime.plusSeconds(1);
        System.out.println(zonedDateTime17);
        ZonedDateTime zonedDateTime18 = zonedDateTime.plusWeeks(1);//增加一周时间
        System.out.println(zonedDateTime18);



    }


    //  时间戳
    private static void instanceTest() {
        //1、获取时间戳，获取的是标准时区的时间戳
        Instant now = Instant.now();
        System.out.println(now);//2025-05-20T02:55:04.994374700Z
        //2、根据指定的时间戳获取Instant 对象
        //根据毫秒值获取Instant对象
        Instant instant = Instant.ofEpochMilli(0L);//1970-01-01T00:00:00Z
        System.out.println(instant);
        ///根据秒值获取Instant对象
        Instant instant1 = Instant.ofEpochSecond(0L);//1970-01-01T00:00:00Z
        System.out.println(instant1);
        //根据毫秒值和纳秒值获取Instant对象,1 秒= 1000毫秒= 1000 * 1000纳秒
        Instant instant2 = Instant.ofEpochSecond(0L, 8 * 60 * 1000);//1970-01-01T00:00:00.000480Z
        System.out.println(instant2);

        //3、指定时区
        ZonedDateTime zonedDateTime = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zonedDateTime);//2025-05-20T11:04:04.144942900+08:00[Asia/Shanghai]

        //4、isXxx 判断
        Instant instant3 = Instant.ofEpochMilli(0L);
        Instant instant4 = Instant.ofEpochMilli(1000L);
        System.out.println(instant3.isBefore(instant4));//判断instant3是否在instant4之前
        System.out.println(instant3.isAfter(instant4));//判断instant3是否在instant4之后
        System.out.println(instant3.equals(instant4));//判断instant3是否等于instant4

        //5、minusXxx 减少
        //减少一天
        Instant minus = Instant.now().minus(1, ChronoUnit.DAYS);
        System.out.println("当前世界时间减去1天" + minus);
        Instant instant5 = minus.minusMillis(1000L);
        System.out.println("减去1s的毫秒值" + instant5);
        Instant instant6 = minus.minusSeconds(60L);
        System.out.println("减去60s" + instant6);
        Instant instant7 = minus.minusNanos(1000L * 1000);//2025-05-19T03:12:31.955405Z
        System.out.println("减去1s的纳秒值" + instant7);

        //6、plusXxx 增加
        Instant plus = Instant.now().plus(1, ChronoUnit.DAYS);//2025-05-21T03:15:53.926200600Z
        System.out.println("当前世界时间加去1天" + plus);

        Instant plus1 = plus.plusMillis(1000L);//2025-05-21T03:15:53.926200600Z
        System.out.println("加上1s的毫秒值" + plus1);
        Instant plus2 = plus.plusSeconds(60L);//2025-05-21T03:16:53.926200600Z
        System.out.println("加上60s" + plus2);
        Instant plus3 = plus.plusNanos(1000L * 1000);//2025-05-21T03:15:53.927200600Z
        System.out.println("加上1s的纳秒值" + plus3);


    }

    // 时区
    private static void ZoneIDTest() {
        //Asia/shanghai  上海时区
        //Asia/taipei    台北时区
        //Asia/chongqing    重庆时区

        ZoneId shanghaiZoneId = ZoneId.of("Asia/Shanghai");
        System.out.println("设置指定时区" + shanghaiZoneId);
        ZoneId zoneId = ZoneId.systemDefault();
        System.out.println("获取系统默认时区" + zoneId);
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
        System.out.println("获取所有时区" + availableZoneIds.size());
        for (String availableZoneId : availableZoneIds) {
            System.out.println(availableZoneId);
        }
    }
}
